/**
 * 
 */
package com.cmcc.xdr.pcapparse.tcp.http;

import java.util.HashMap;
import java.util.Map.Entry;
import java.util.concurrent.atomic.AtomicInteger;

import com.cmcc.awareness.AppInfo;
import com.cmcc.xdr.util.statistics.Statistics;

/**
 * @Date 2018年4月18日 下午12:05:07
 * @author Amish
 */
public class HttpProtoDistinguishStatistics  implements Statistics{
	
	private static HttpProtoDistinguishStatistics  instance= new HttpProtoDistinguishStatistics();
	public static HttpProtoDistinguishStatistics getInstance() {
		return instance;
	}
	
	private AtomicInteger distinguishNum;
	private AtomicInteger unDistinguishNum;
	private HashMap<String,AtomicInteger> httpProtoAppDistinguishStat;
	
	
	private HttpProtoDistinguishStatistics() {
		distinguishNum =  new AtomicInteger(0);
		unDistinguishNum = new AtomicInteger(0);
		httpProtoAppDistinguishStat = new HashMap<String,AtomicInteger>();
	}
	
	public synchronized void incrementUnDistinguish() {
		unDistinguishNum.incrementAndGet();
	}

	public synchronized void incrementDistinguish(AppInfo appInfo) {
		distinguishNum.incrementAndGet();
		
		if(httpProtoAppDistinguishStat.containsKey(appInfo.getApp_subtype_name())) {
			httpProtoAppDistinguishStat.get(appInfo.getApp_subtype_name()).incrementAndGet();
		}else {
			httpProtoAppDistinguishStat.put(appInfo.getApp_subtype_name(), new AtomicInteger(1));
		}
	}
	
	/**
	 * 统计数据清零
	 */
	public void reset() {
		distinguishNum.set(0);;
		unDistinguishNum.set(0);
		httpProtoAppDistinguishStat.clear();;
	}
	
	public String toString() {
		StringBuilder sb = new StringBuilder();
		for( Entry<String,AtomicInteger> entry : httpProtoAppDistinguishStat.entrySet() ) {
			sb.append("  [ "+entry.getKey()+" ]  : ");
			sb.append(entry.getValue().get());
			sb.append("\n");
		}
		sb.append("  [ total distinguish number ]  : "+distinguishNum);
		sb.append("\n");
		sb.append("  [ total undistinguish number ]  : "+unDistinguishNum);
		return sb.toString();
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
